package com.liuceng.zen.web.controller;

import com.liuceng.zen.constants.Constants;
import com.liuceng.zen.pojo.domain.CustomerInfoMailNotification;
import com.liuceng.zen.pojo.domain.Member;
import com.liuceng.zen.pojo.form.CustomerInfoNotificationForm;
import com.liuceng.zen.service.CustomerInfoMailNotificationService;
import com.liuceng.zen.service.MemberService;
import com.liuceng.zen.web.auth.realm.ShiroDbRealm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liyuhang on 2016-1-9.
 */
@Controller
public class CustomerInfoNotificationController extends BaseController {
  @Autowired
  private CustomerInfoMailNotificationService notificationService;
  @Autowired
  private MemberService memberService;
  @Value("${weixinqr}")
  private String weixinqr;



  @RequestMapping(value = "/ci/list", method = RequestMethod.GET)
  public String index(@ModelAttribute("form") CustomerInfoNotificationForm notificationForm) {
    ShiroDbRealm.UserContext userContext = getUserContext();
    Integer memberId = userContext.getId();
    boolean isFromBackend = userContext.isFromBackend();
    // 获取email列表
    Map<String, Object> params = new HashMap<>();
    params.put("emailType", isFromBackend ? CustomerInfoMailNotification.MAIL_TYPE_BACKEND
        : CustomerInfoMailNotification.MAIL_TYPE_FRONT);
    params.put("memberId", memberId);
    //
    notificationForm.setEmails(notificationService.findEmailList(params));
    notificationForm.setWeixinqr(weixinqr);

    return "ci/list";
  }

  @RequestMapping(value = "/ci/do_add_email", method = RequestMethod.POST)
  public String doAddEmail(@ModelAttribute("form") CustomerInfoNotificationForm notificationForm,
      RedirectAttributes redirectAttributes) {
    String email = notificationForm.getEmail();
    ShiroDbRealm.UserContext userContext = getUserContext();
    Integer memberId = userContext.getId();
    boolean isFromBackend = userContext.isFromBackend();
    //
    Integer mailType =
        isFromBackend ? CustomerInfoMailNotification.MAIL_TYPE_BACKEND
            : CustomerInfoMailNotification.MAIL_TYPE_FRONT;
    //
    Integer count = notificationService.countByEmailAndEmailType(email, mailType, memberId);

    if (count == 0) {
      CustomerInfoMailNotification notification = new CustomerInfoMailNotification();
      notification.setEmail(email);
      notification.setEmailType(mailType);
      //
      notification.setMemberId(memberId);
      Member member = memberService.findMember(memberId);
      notification.setAdminId(member.getAdminId());
      notification.setDepartmentId(member.getAdminDepartmentId());
      //
      notificationService.insert(notification);
    }

    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "添加成功");
    return "redirect:/ci/list";
  }

  @RequestMapping(value = "/ci/delete_email/{id}", method = RequestMethod.POST)
  public String doDelEmail(@PathVariable Integer id, RedirectAttributes redirectAttributes) {
    notificationService.delete(id);
    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "删除成功");
    return "redirect:/ci/list";
  }
}
